package crmdna.client;

import java.util.List;

import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;

import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.crmdna.Client;
import crmdna.crmdna.ClientEntity;

@Api(name = "client")
public class ClientApi {
	@ApiMethod(path="getall", httpMethod = HttpMethod.GET)
	public APIResponse getAll() {
		
		try {
			List<ClientEntity> all = Client.getAll();
						
			return new APIResponse(Status.SUCCESS, all);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="create", httpMethod = HttpMethod.PUT)
	public APIResponse create(@Named("name") String name) {
		
		try {
			ClientEntity clientEntity = Client.create(name);			
			
			return new APIResponse(Status.SUCCESS, clientEntity);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="updateDisplayName", httpMethod = HttpMethod.POST)
	public APIResponse updateDisplayName(@Named("name") String name, 
			@Named("new_display_name") String newDisplayName) {
		
		try {
			ClientEntity clientEntity = Client.updateDisplayName(name, newDisplayName);
			
			return new APIResponse(Status.SUCCESS, clientEntity);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
}
